package com.yami.shop.admin.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.UserMapper;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.yami.shop.service.PostRecordService;
import com.yami.shop.bean.model.PostRecord;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 * 动态记录 前端控制器
 * </p>
 *
 * @author chajiu
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/post-record")
@AllArgsConstructor
public class PostRecordController {


    @Autowired
    private PostRecordService PostRecordService;
    @Autowired
    private UserMapper userMapper;

    private final MapperFacade mapperFacade;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin:PostRecord:page')")
    public ResponseEntity<IPage<PostRecord>> page(PostRecord postRecord, PageParam<PostRecord> page) {
        IPage<PostRecord> iPage = PostRecordService.page(page, new LambdaQueryWrapper<PostRecord>()
                .orderByDesc(PostRecord::getCreateTime));
        for(PostRecord pages : iPage.getRecords()){
            User user = userMapper.selectById(pages.getUserId());
            pages.setUserName(user.getNickName());
            pages.setUserPic(user.getPic());
        }
        return ResponseEntity.ok(iPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('admin:PostRecord:info')")
    public ResponseEntity<PostRecord> info(@PathVariable("id") String id) {
        PostRecord PostRecord = PostRecordService.getById(id);
        User user = userMapper.selectById(PostRecord.getUserId());
        PostRecord.setUserName(user.getNickName());
        PostRecord.setUserPic(user.getPic());
        return ResponseEntity.ok(PostRecord);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('admin:PostRecord:save')")
    public ResponseEntity<Void> save(@RequestBody PostRecord postRecord) {
        postRecord.setId(IdUtil.simpleUUID());
        PostRecordService.save(postRecord);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:PostRecord:update')")
    public ResponseEntity<Void> update(@RequestBody PostRecord postRecord) {
        PostRecordService.updateById(postRecord);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin:PostRecord:delete')")
    public ResponseEntity<Void> delete(@RequestBody String[] ids) {
        PostRecordService.removeByIds(Arrays.asList(ids));
        return ResponseEntity.ok().build();
    }


}

